package com.ruoyi.controller.open;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.DownloadVideoDTO;
import com.ruoyi.common.core.domain.dto.VehicleInfoDTO;
import com.ruoyi.common.core.domain.dto.VehiclePicDTO;
import com.ruoyi.common.core.domain.dto.VehicleVideoDTO;
import com.ruoyi.hkCamera.service.VideoService;
import com.ruoyi.service.QuUploadService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/open/qu/")
public class QuOpenController extends BaseController {

    @Autowired
    private IFltVehicleInfoService fltVehicleInfoService;

    @Autowired
    private IFltVehiclePicService fltVehiclePicService;

    @Autowired
    private IFltVehicleVideoService fltVehicleVideoService;

    @Autowired
    private IZcVehicleService zcVehicleService;

    @Autowired
    private IZcVehicleVideoService zcVideoService;

    @Autowired
    private VideoService hkVideoService;

    @Autowired
    private QuUploadService quUploadService;

    @GetMapping("/getUploadVideo")
    public AjaxResult getUploadVideo(@RequestParam("checkDate") String checkDate) {
        List<ZcVehicle> overWeightList = zcVehicleService.getOverWeightList(checkDate);
        for (ZcVehicle vehicle : overWeightList) {
            vehicle.setCheckDate(checkDate);
            String videoName = zcVideoService.getVideoName(vehicle.getCheckNumber(), vehicle.getLaneNo());
            String videoUrl = zcVideoService.getVideoUrl(videoName, vehicle.getCheckTime());
            // 创建Path对象
            Path path = Paths.get(videoUrl);
            // 检查文件是否存在
            if (!Files.exists(path)) {
                zcVideoService.saveUpload(vehicle);
            }
        }
        return success();
    }

    @GetMapping("/downloadVideo")
    public AjaxResult downloadVideo(@RequestParam("checkDate") String checkDate) {
        List<ZcVehicleVideo> uploadList = zcVideoService.getUploadList(checkDate);
        for (ZcVehicleVideo video : uploadList) {
            // 创建Path对象
            Path path = Paths.get(video.getVideoUrl());
            // 检查文件是否存在
            if (!Files.exists(path)) {
                String laneNo = StrUtil.subAfter(video.getVideoName(), "-", false);
                DownloadVideoDTO dto = new DownloadVideoDTO();
                dto.setCheckNo(video.getCheckNumber());
                dto.setLaneNo(laneNo);
                dto.setCheckTime(video.getCheckTime());
                dto.setVideoName(video.getVideoName() + ".mp4");
                dto.setVideoDirs(StrUtil.replace(video.getVideoUrl(), dto.getVideoName(), ""));
                hkVideoService.downloadVideo(dto);
            }
        }
        return success();
    }

    @GetMapping("/uploadVideo")
    public AjaxResult uploadVideo(@RequestParam("checkDate") String checkDate) {
        List<ZcVehicleVideo> uploadList = zcVideoService.getUploadList(checkDate);
        for (ZcVehicleVideo video : uploadList) {
            VehicleVideoDTO vehicleVideo = JSONUtil.toBean(video.getRemarks(), VehicleVideoDTO.class);
            if (StrUtil.isBlank(vehicleVideo.getFileUrl())) {
                vehicleVideo.setFileUrl(video.getVideoUrl());
            }
            if (StrUtil.isBlank(vehicleVideo.getFileName())) {
                vehicleVideo.setFileName(video.getVideoName());
            }
            boolean uploadSuccess = quUploadService.uploadVideo(vehicleVideo);
            if (uploadSuccess) {
                zcVideoService.updateUpload(video.getCheckNumber());
            }
        }
        return success();
    }

    /**
     * 藤县上传数据
     */
    @GetMapping("/tengxian/uploadData")
    public AjaxResult uploadData(@RequestParam("checkNo") String checkNo) {
        if (StrUtil.isBlank(checkNo)) {
            return error("checkNo不能为空");
        }
        FltVehicleInfo vehicleInfo = fltVehicleInfoService.getByCheckNo(checkNo);
        if (vehicleInfo == null) {
            return error("数据不存在");
        }

        // 上传称重数据
        quUploadService.uploadWeight(BeanUtil.copyProperties(vehicleInfo, VehicleInfoDTO.class));

        FltVehicleVideo video = fltVehicleVideoService.getByCheckNo(checkNo);
        if (video != null) {
            // 上传视频
            quUploadService.uploadVideo(BeanUtil.copyProperties(video, VehicleVideoDTO.class));
        }

        List<FltVehiclePic> picList = fltVehiclePicService.getByCheckNo(checkNo);
        for (FltVehiclePic pic : picList) {
            // 上传图片
            quUploadService.uploadPic(BeanUtil.copyProperties(pic, VehiclePicDTO.class));
        }

        return success();
    }
}
